From d6ce65f81c2914640746e9ae8d12e0096da6ab11 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 11 Sep 2021 20:27:13 -0400 Subject: [PATCH] Add tests for the png loader --- testsuite/gdk/image-data/image.png | Bin 0 -> 1609 bytes testsuite/gdk/image.c | 118 +++++++++++++++++++++++++++++ testsuite/gdk/meson.build | 26 +++++++ 3 files changed, 144 insertions(+) create mode 100644 testsuite/gdk/image-data/image.png create mode 100644 testsuite/gdk/image.c diff --git a/testsuite/gdk/image-data/image.png b/testsuite/gdk/image-data/image.png new file mode 100644 index 0000000000000000000000000000000000000000..49ee0d459eb88075379a3d35327bb4e5401d6453 GIT binary patch literal 1609 zcmV-P2DbT$P)@ge=+_a0E5O2H(YzNFZ>6klu^S81&uB=A=GmmTzT6qci;23?q|@@n{VG| zpM9gfdiH`Fi6Y{`Sp8~AhP>-5;GbRl! zLq;@Y$AA9rGf)t_$KY?>Z|lH?7oXdF#F59jyPW(GvU*Phb11eQnu#s@3WTW-DW!HG ztQJxReWOL|zMU7X`bp_vRZ}S{y&SuC(TpGQ@@+@q&DUPSLSC9 zV5`19CXXMBin0oXLZOsMso-7Xsm$#vfd;5ZZ``WB*5pJ@5IS~Mkm9e5Pu2bTud0B~*Zvyb1i zr!|>a^5-XdJVnaZ0RXD8As%!8eB&}i!V#T;Fh2bF1IXZn5>$(_KM5cw-GwKChGv0= zgakm*yDSgQo`5hx3I#4z;EY;6%+;^X*z>(4Hg4JojYKK{$>kMqzV+qvBrcH33x{ZAcp)bVGuEyb%BL?eIHUu7(_HD9LLc@wJ-=!9h75(Vvcog*9rh! z5perdrAfC9An7IX@M8~S>utBj{CNv<3JPFFm~Z7hEJ;2}dudV0EL{J?=3 zE#(y|AQB5P?Z^RRMl(1}2ScePn1TY~=I#X)0VH@pT9P6m0m1kQV^LQZLw;UipmycXXWyx%a&vQf`wL0+l=+;*K_eBkKBKTUfYS?Ze)OH7!`CK0J-O1 zclSVzxv2xIEzTW?a6wwSmiDDsKt+&P=a>yZ~>i`01hcY`mTLgxid-m+P?uYMwo={v|9F4_dX3o)Cv}kDPHSUo| z9d>a+QQ@PyTxaNdkq$r-0!ayZ-#G8q72I;`4d3{j?@$}7M^RBBHf-30($eB~dgI2; z2!|P>(KI9zNlQ*WZrhZS4+r!ce29kEEfQXI>G=l;DJSf)#~v6ybTAmDDAz_Q&`ysW zJ_P&hy*I{>9fRF>+XcgQUrC8AwwPps+3EKwzwg{_5}tG3S?f+d@szTG0|(-7fPN+O z@%)R=t$XPHM=H=7U2`BFk2l|b=WW2>4)c-YqZ9o9zvI6E#hK6du_U**00000NkvXX Hu0mjfn2831 literal 0 HcmV?d00001 diff --git a/testsuite/gdk/image.c b/testsuite/gdk/image.c new file mode 100644 index 0000000000..0a916eb404 --- /dev/null +++ b/testsuite/gdk/image.c @@ -0,0 +1,118 @@ +#include +#include "gdk/loaders/gdkpngprivate.h" + +static void +assert_texture_equal (GdkTexture *t1, + GdkTexture *t2) +{ + int width; + int height; + int stride; + guchar *d1; + guchar *d2; + + width = gdk_texture_get_width (t1); + height = gdk_texture_get_height (t1); + stride = 4 * width; + + g_assert_cmpint (width, ==, gdk_texture_get_width (t2)); + g_assert_cmpint (height, ==, gdk_texture_get_height (t2)); + + d1 = g_malloc (stride * height); + d2 = g_malloc (stride * height); + + gdk_texture_download (t1, d1, stride); + gdk_texture_download (t2, d2, stride); + + g_assert_cmpmem (d1, stride * height, d2, stride * height); + + g_free (d1); + g_free (d2); +} + +static void +test_load_image (gconstpointer data) +{ + const char *filename = data; + GdkTexture *texture; + char *path; + GFile *file; + GBytes *bytes; + GError *error = NULL; + + path = g_test_build_filename (G_TEST_DIST, "image-data", filename, NULL); + file = g_file_new_for_path (path); + bytes = g_file_load_bytes (file, NULL, NULL, &error); + g_assert_no_error (error); + + if (g_str_has_suffix (filename, ".png")) + texture = gdk_load_png (bytes, &error); + else + g_assert_not_reached (); + + g_assert_no_error (error); + g_assert_true (GDK_IS_TEXTURE (texture)); + g_assert_cmpint (gdk_texture_get_width (texture), ==, 32); + g_assert_cmpint (gdk_texture_get_height (texture), ==, 32); + + g_object_unref (texture); + g_bytes_unref (bytes); + g_object_unref (file); + g_free (path); +} + +static void +test_save_image (gconstpointer test_data) +{ + const char *filename = test_data; + char *path; + GFile *file; + GdkTexture *texture; + GFile *file2; + GdkTexture *texture2; + GError *error = NULL; + GBytes *bytes = NULL; + GIOStream *stream; + + path = g_test_build_filename (G_TEST_DIST, "image-data", filename, NULL); + file = g_file_new_for_path (path); + texture = gdk_texture_new_from_file (file, &error); + g_assert_no_error (error); + + if (g_str_has_suffix (filename, ".png")) + bytes = gdk_save_png (texture); + else + g_assert_not_reached (); + + file2 = g_file_new_tmp ("imageXXXXXX", (GFileIOStream **)&stream, NULL); + g_object_unref (stream); + g_file_replace_contents (file2, + g_bytes_get_data (bytes, NULL), + g_bytes_get_size (bytes), + NULL, FALSE, 0, + NULL, NULL, &error); + g_assert_no_error (error); + + texture2 = gdk_texture_new_from_file (file2, &error); + g_assert_no_error (error); + + assert_texture_equal (texture, texture2); + + g_bytes_unref (bytes); + g_object_unref (texture2); + g_object_unref (file2); + g_object_unref (texture); + g_object_unref (file); + g_free (path); +} + +int +main (int argc, char *argv[]) +{ + (g_test_init) (&argc, &argv, NULL); + + g_test_add_data_func ("/image/load/png", "image.png", test_load_image); + g_test_add_data_func ("/image/save/png", "image.png", test_save_image); + + return g_test_run (); +} diff --git a/testsuite/gdk/meson.build b/testsuite/gdk/meson.build index 3eff25868d..c259be1f06 100644 --- a/testsuite/gdk/meson.build +++ b/testsuite/gdk/meson.build @@ -49,6 +49,31 @@ foreach t : tests ) endforeach +internal_tests = [ + 'image' +] + +foreach t : internal_tests + test_exe = executable(t, '@0@.c'.format(t), + c_args: common_cflags, + dependencies: libgtk_static_dep, + install: get_option('install-tests'), + install_dir: testexecdir, + ) + + test(t, test_exe, + args: [ '--tap', '-k' ], + protocol: 'tap', + env: [ + 'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()), + 'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()), + 'DBUS_SESSION_BUS_ADDRESS=', + ], + suite: 'gdk', + ) +endforeach + + if get_option('install-tests') foreach t : tests test_cdata = configuration_data() @@ -63,4 +88,5 @@ if get_option('install-tests') endforeach install_subdir('clipboard-data', install_dir: testexecdir) + install_subdir('image-data', install_dir: testexecdir) endif -- 2.30.2